#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <pspgu.h>
#include <string.h>
#include <stdio.h>

#include <pspimpose_driver.h>
#include <psputility.h>
#include "OSK.h"

#define TEXT_LENGTH			(128)

/**
 *  All sceUtilities except MsgDialog don't like it when you exit im them.
 */

OSK::OSK(Graph *gu)
{
	graph = gu;
}

void OSK::ExitCallbackEnable() {
        sceImposeSetHomePopup(1);
}
void OSK::ExitCallbackDisable() {
        sceImposeSetHomePopup(0);
}

void OSK::asciiToUnicode(char *from, unsigned short *to) {
        int c;
        while( (c = *from) ) {
                *to = (unsigned short)c;
                from++;
                to++;
        }
        *to = '\0';
}

void OSK::unicodeToAscii(unsigned short *from, char *to) {
        int c;
        while( (c = *from) ) {
                *to = (char)c;
                to++;
                from++;
        }
        *to = '\0';
}

char* OSK::getKeyboard(int max, int column, char *pretext, char *current) 
{
        SceUtilityOskData data;
        SceUtilityOskParams params;
        unsigned short in[1024];
        unsigned short out[1024];
        unsigned short desc[128];
        int done = 0;
        
        if(pretext)
                asciiToUnicode(pretext,desc);
        else
                *desc = '\0';
        if(current)
                asciiToUnicode(current,in);
        else
                *in = '\0';
        *out = '\0';

        memset(&data, 0, sizeof(SceUtilityOskData));
        data.language = PSP_UTILITY_OSK_LANGUAGE_DEFAULT; // Use system default for text input
        data.lines = 1;
        data.unk_24 = 1;
        if(strcmp(pretext, "Name: ") == 0) 
                data.inputtype = PSP_UTILITY_OSK_INPUTTYPE_LATIN_DIGIT;
        else 
                data.inputtype = PSP_UTILITY_OSK_INPUTTYPE_ALL; // Allow all input types
        data.desc = desc;
        data.intext = in;
        data.outtextlength = 1024;
        data.outtextlimit = max; // Limit input to 32 characters
        data.outtext = out;

        memset(&params, 0, sizeof(params));
        params.base.size = sizeof(params);
        sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_LANGUAGE, &params.base.language);
        sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_UNKNOWN, &params.base.buttonSwap);
        params.base.graphicsThread = 17;
        params.base.accessThread = 19;
        params.base.fontThread = 18;
        params.base.soundThread = 16;
        params.datacount = 1;
        params.data = &data;

        ExitCallbackDisable();
        printf("starting OSK\n");
        sceUtilityOskInitStart(&params);

        while(!done) 
		{
				graph->RefreshScreen(0xff554433);	

                switch(sceUtilityOskGetStatus()) {
                        case PSP_UTILITY_DIALOG_INIT:
                                break;

                        case PSP_UTILITY_DIALOG_VISIBLE:
                                sceUtilityOskUpdate(1);
                                break;

                        case PSP_UTILITY_DIALOG_QUIT:
                                sceUtilityOskShutdownStart();
                                break;

                        case PSP_UTILITY_DIALOG_FINISHED:
                                break;

                        case PSP_UTILITY_DIALOG_NONE:
                                done = 1;
                                break;

                        default :
                                break;
                }

                sceDisplayWaitVblankStart();
                sceGuSwapBuffers();
        }
        ExitCallbackEnable();
        printf("exiting OSK\n");
        unicodeToAscii(out, current);
        return current;
}